package br.com.pontov;
 
import java.util.Vector;

import br.com.pontov.pieces.Ball;
import br.com.pontov.pieces.Pad;
import br.com.pontov.pieces.Wall;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
 
public class GameView extends View implements Runnable{
	
	private PiecesManager pieces;
	private int width = 600, height = 1024;//Tamanho do Galaxy Tab
	private Paint background;
	private Handler handler;
	private Pad northPad;
	private Pad southPad;
	private Bitmap image;
	
   public GameView(Context context) {
      super(context);
      init();
   }
   public GameView(Context context, AttributeSet attrs) {
      super(context, attrs);
      init();
   }
   public GameView(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
      init();
   }
   private void init() {
      //Metodos para inicializar os componentes da nossa tela
	   background = new Paint();
	   background.setColor(Color.BLACK);
	 
	   pieces = new PiecesManager();
	 

	   northPad = new Pad(new Vector2D(0, -1), 80); //80 é a distancia default do eixo, no caso desenhar no 80 de y
	   pieces.addPiece(northPad);
	   southPad = new Pad(new Vector2D(0, 1), 944);//944 do y
	   pieces.addPiece(southPad);
	   pieces.addPiece(new Wall(new Rect(0, 0, 10, height), new Vector2D(1,0)));//Left
	   pieces.addPiece(new Wall(new Rect(width - 10, 0, width, height), new Vector2D(-1,0)));//Right
	   pieces.addPiece(new Wall(new Rect(0, 0, width, 10), new Vector2D(0,-1)));// Top
	   pieces.addPiece(new Wall(new Rect(0, height - 10, width, height),new Vector2D(0,1)));//Bottom
	   
	   image = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
	   pieces.addPiece(new Ball(image,new Vector2D(300, 600), new Vector2D(1, 1), 8,pieces));
	   
   }
   public void onDraw(Canvas canvas){
      //Desenhar na tela
	   
	   canvas.save();
	 //Deixando a bolinha se pintar na posição correta
	    canvas.drawRect(0, 0, getWidth(), getHeight(), background);
	    //a classe gerenciadora do componentes da tela desenha
	    pieces.draw(canvas);
	    //Guardar o estado do Canvas
	    canvas.restore();
   }
   
   public void run() {
	// TODO Auto-generated method stub
	while (true) {
        try {
            pieces.processAI();

            Message msg = new Message();
            msg.what = 10001;//Mesmo id que o Handler espera para atualizar
            handler.sendMessage(msg);

            Thread.sleep(10);
        } catch (Exception e) {
        }
    }
   }

   public void setCallbackHandler(Handler guiRefresher) {
	   this.handler = guiRefresher;
   }
   
   @Override
   public boolean onTouchEvent(MotionEvent evt) {
	   for (int i = 0; i < evt.getPointerCount(); i++) {
	        float y = evt.getY(i);
	        if (y > 612)
	            southPad.notifyMotionEvent(evt.getX(i), evt.getY(i));
	        else
	            northPad.notifyMotionEvent(evt.getX(i), evt.getY(i));
	    }
	 
	    return true;
	}

}